/**
 *******************************************************************************
 * @FileName  : mem_pool.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-05-18
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#ifndef __MEM_POOL_H_
#define __MEM_POOL_H_

#include "defs_types.h"

#define gp_printf           MEM_TRACK_INFO
#define MEM_POOL_TEST_EN    0

#define MEM_POOL_MIN_SIZE   4

typedef uint16_t  PoolSize_t;

typedef struct item_s{
  struct item_s *pNext;
}item_t;

/*! memory pool structure */
typedef struct{
    uint8_t     *pBuf;
    item_t      *pFree;
    PoolSize_t   size;
    PoolSize_t   num;
    uint32_t     freeCnt;//for debug
}MemPool_t;

/**
 * @brief : memory pool initialization.
 * @param : pool
 * @param : pBuf     pointer point to memory
 * @param : size     the size of buffer in pool. must 4B align.
 * @param : num      the number of buffer in pool.
 * @return: -1 if failed or 0.
 */
int MemPoolInit(MemPool_t *pool, uint8_t *pBuf, PoolSize_t size, PoolSize_t num);

/**
 * @brief : memory pool allocate buffer.
 * @param : pool
 * @param : len
 * @return: NULL if failed
 */
void *MemPoolAlloc(MemPool_t *pool, PoolSize_t len);

/**
 * @brief : memory pool free.
 * @param : pool
 * @param : item
 * @return: -1 if failed or 0.
 */
int MemPoolFree(MemPool_t *pool, void *item);

/**
 * @brief : Check if memory block from pool
 * @param : pool
 * @param : item
 * @return:
 */
bool_t MemPoolIsBlockFrom(MemPool_t *pool, void *item);

#if MEM_POOL_TEST_EN
void MemPool_Test(void);
#endif

#endif
